package com.candy.db.generator.main;

import com.candy.db.generator.config.GlobalConfig;
import com.candy.db.generator.core.Generator;
import com.candy.db.generator.utils.LogKit;
import com.candy.db.generator.utils.StrKit;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.io.File;
import java.security.InvalidParameterException;
import java.sql.SQLException;

/**
 * Created by yantingjun on 2016/7/12.
 */
public class MainRunner {

    public static void main(String[] args) throws SQLException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass(GlobalConfig.dbDriverName);
            ds.setJdbcUrl(GlobalConfig.dbConnectionUrl);
            ds.setUser(GlobalConfig.dbUsername);
            ds.setPassword(GlobalConfig.dbPassword);
            ds.setLogWriter(null);
        } catch (PropertyVetoException e) {
            LogKit.debug("Invalid driver class !",e);
        }

        CommandParam param = null;
        try{
            param = CommandParamParser.parse(args);
        }catch (InvalidParameterException e){
            LogKit.error(e.getMessage());
            return;
        }

        if(param.overrideForce!=null){
            GlobalConfig.overrideForce = param.overrideForce;
        }
        if(StrKit.isNotBlank(param.templateGroup)){
            GlobalConfig.templateGroup = param.templateGroup;
        }
        if(StrKit.isNotBlank(param.modelPackageName)){
            GlobalConfig.modelPackageName = param.modelPackageName;
        }
        if(StrKit.isNotBlank(param.baseModelPackageName)){
            GlobalConfig.baseModelPackageName = param.baseModelPackageName;
        }
        if(StrKit.isBlank(GlobalConfig.baseModelPackageName) && StrKit.isNotBlank(GlobalConfig.modelPackageName)){
            GlobalConfig.baseModelPackageName = GlobalConfig.modelPackageName+"."+GlobalConfig.DEFAULT_BASE_MODEL_DIR;
        }
        if(StrKit.isNotBlank(param.daoPackageName)){
            GlobalConfig.daoPackageName = param.modelPackageName;
        }
        if(StrKit.isNotBlank(param.outDir)){
            GlobalConfig.outDir = param.outDir;
        }
        if(StrKit.isBlank(GlobalConfig.outDir)){
            GlobalConfig.outDir = new File("").getAbsolutePath();
        }

        if(StrKit.isNotBlank(param.daoOutDir)){
            GlobalConfig.daoOutDir = param.daoOutDir;
        }
        if(StrKit.isBlank(GlobalConfig.daoOutDir)){
            GlobalConfig.daoOutDir = GlobalConfig.outDir+ File.separator+ GlobalConfig.DEFAULT_DAO_DIR;
        }

        if(StrKit.isNotBlank(param.modelOutDir)){
            GlobalConfig.modelOutDir = param.modelOutDir;
        }
        if(StrKit.isBlank(GlobalConfig.modelOutDir)){
            GlobalConfig.modelOutDir = GlobalConfig.outDir+ File.separator+ GlobalConfig.DEFAULT_MODEL_DIR;
        }

        if(StrKit.isNotBlank(param.baseModelOutDir)){
            GlobalConfig.baseModelOutDir = param.baseModelOutDir;
        }
        if(StrKit.isBlank(GlobalConfig.baseModelOutDir)){
            GlobalConfig.baseModelOutDir = GlobalConfig.modelOutDir+ File.separator+ GlobalConfig.DEFAULT_BASE_MODEL_DIR;
        }

        Generator generator = new Generator(ds,
                GlobalConfig.baseModelPackageName,GlobalConfig.baseModelOutDir,
                GlobalConfig.modelPackageName,GlobalConfig.modelOutDir,
                GlobalConfig.daoPackageName,GlobalConfig.daoOutDir);

        if(param.excludeTables!=null){
            generator.addExcludedTable(param.excludeTables.toArray(new String[]{}));
        }
        if(StrKit.isNotBlank(GlobalConfig.excludeTables)){
            generator.addExcludedTable(GlobalConfig.excludeTables.split(","));
        }
        generator.setRemovedTableNamePrefixes(GlobalConfig.removedTableNamePrefixes);

        generator.generate();
    }
}
